package net.sf.sbv.ui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * smsBackupReaderGUI.java
 *
 * Created on Jul 30, 2011, 11:48:28 AM
 */
import java.io.File;
import java.util.Collection;
import javax.swing.JFileChooser;
import net.sf.sbv.helpers.Utils;
import net.sf.sbv.modals.SmsFileDetail;
import net.sf.sbv.modals.Contact;
import javax.swing.JOptionPane;
import javax.swing.UIManager;


/**
 *
 * @author xtnetworks
 */
public class NewGUI extends javax.swing.JFrame {
    public static NewGUI NEW_GUI;
    private File openFile;

    private SmsFileDetail currentSmsDetail;
    private SmsFileDetail originalSmsDetail;
    
    /** Creates new form smsBackupReaderGUI */
    public NewGUI() {
        initComponents();
        showClearFilter();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jLabel2 = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        contactListBox = new javax.swing.JList();
        messagesPanel = new MultipleMessagePanel();
        lbNumberOfMessage = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        fileLocationField = new javax.swing.JTextField();
        chooseButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        cmdGo = new javax.swing.JButton();
        cmdClearFilter = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        aboutMenuButton = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SMS Backup Viewer - Eighty_Coffee");

        jLabel2.setText("Number of SMS:");

        jSplitPane1.setDividerLocation(250);

        contactListBox.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        contactListBox.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                contactListBoxValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(contactListBox);

        jSplitPane1.setLeftComponent(jScrollPane1);
        jSplitPane1.setRightComponent(messagesPanel);

        lbNumberOfMessage.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbNumberOfMessage.setText("Not Loaded");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        fileLocationField.setEditable(false);
        fileLocationField.setText("<Select any file>");
        fileLocationField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileLocationFieldMouseClicked(evt);
            }
        });

        chooseButton.setText("...");
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(fileLocationField, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chooseButton))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(fileLocationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(chooseButton))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jTextField1.setText("Search");
        jTextField1.setEnabled(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        cmdGo.setText("Go");
        cmdGo.setEnabled(false);
        cmdGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdGo))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cmdGo))
        );

        cmdClearFilter.setText("Clear Filter");
        cmdClearFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdClearFilterActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Open File");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        jMenu2.setText("Help");

        aboutMenuButton.setText("About...");
        aboutMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuButtonActionPerformed(evt);
            }
        });
        jMenu2.add(aboutMenuButton);

        menuBar.add(jMenu2);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbNumberOfMessage)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdClearFilter)
                .addContainerGap(623, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbNumberOfMessage)
                    .addComponent(cmdClearFilter))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("SMS Backup Reader v0.2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        openFileChooser();
    }//GEN-LAST:event_chooseButtonActionPerformed

    private void contactListBoxValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_contactListBoxValueChanged
        Contact selectedContact = (Contact) contactListBox.getSelectedValue();

        if(selectedContact != null) {
            ((MultipleMessagePanel)messagesPanel).setContact(selectedContact);
        }

    }//GEN-LAST:event_contactListBoxValueChanged

    private void aboutMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuButtonActionPerformed
        About.showAbout(this);
    }//GEN-LAST:event_aboutMenuButtonActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void cmdGoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGoActionPerformed
        searchKeyword();
    }//GEN-LAST:event_cmdGoActionPerformed

    private void cmdClearFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdClearFilterActionPerformed
        currentSmsDetail = originalSmsDetail;
        populateData();
    }//GEN-LAST:event_cmdClearFilterActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        searchKeyword();
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void fileLocationFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileLocationFieldMouseClicked
        openFileChooser();
    }//GEN-LAST:event_fileLocationFieldMouseClicked

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        openFileChooser();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    NEW_GUI = new NewGUI();
                    NEW_GUI.setVisible(true);
                } catch(Exception e) {}
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JButton cmdClearFilter;
    private javax.swing.JButton cmdGo;
    private javax.swing.JList contactListBox;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JTextField fileLocationField;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel lbNumberOfMessage;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel messagesPanel;
    // End of variables declaration//GEN-END:variables

    private void loadFileData() {
        if(openFile != null) {
            try {
                currentSmsDetail = Utils.parseAndGetSmsFileDetails(openFile);
                fileLocationField.setText(openFile.getAbsolutePath());
                
                originalSmsDetail = currentSmsDetail;
                populateData();
            } catch(Exception e) {
                JOptionPane.showMessageDialog(this, "Error loading file. System says:\n"
                        + e.getLocalizedMessage() + "\n"
                        + "Please load the correct file.", "Error loading file", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void populateData() {
        if (currentSmsDetail != null) {
            lbNumberOfMessage.setText(currentSmsDetail.getNumberOfMessages() + "");
            Collection tempColl = currentSmsDetail.getSmsData().values();
            Contact[] contactArray = (Contact[]) tempColl.toArray(new Contact[tempColl.size()]);
            contactListBox.setListData(contactArray);
        }

        //enable disable searching
        boolean enableSearch = originalSmsDetail != null && originalSmsDetail.getNumberOfMessages() > 0;

        jTextField1.setEnabled(enableSearch);
        cmdGo.setEnabled(enableSearch);

        showClearFilter();
    }

    private void searchKeyword() {
        cmdGo.setEnabled(false);
        SmsFileDetail searchResult = Utils.searchKeyword(jTextField1.getText(), originalSmsDetail);

        if(searchResult != null && searchResult.getNumberOfMessages() > 0) {
            currentSmsDetail = searchResult;
        } else {
            currentSmsDetail = new SmsFileDetail();
        }

        ((MultipleMessagePanel)messagesPanel).clearPreviousData();
        populateData();
    }

    private void showClearFilter() {
        cmdClearFilter.setVisible(currentSmsDetail != originalSmsDetail);
    }

    private void openFileChooser() {
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            openFile = fileChooser.getSelectedFile();
            loadFileData();
        }
    }

    public SmsFileDetail getOriginalSmsDetail() {
        return originalSmsDetail;
    }
}
